package com.school.platform.servlet;

import com.school.platform.utils.DBConnectionUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/infoDetail") // 访问路径：/infoDetail?infoId=1
public class InfoDetailServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 设置请求编码
        request.setCharacterEncoding("UTF-8");
        // 设置响应编码
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        // 获取信息ID
        int infoId = Integer.parseInt(request.getParameter("infoId"));

        // 获取信息详情
        InfoDetail infoDetail = getInfoDetail(infoId);
        // 获取评论列表
        List<Comment> commentList = getCommentList(infoId);
        // 获取点赞数
        int likeCount = getLikeCount(infoId);

        // 数据存入request
        request.setAttribute("infoDetail", infoDetail);
        request.setAttribute("commentList", commentList);
        request.setAttribute("likeCount", likeCount);
        // 转发到JSP
        request.getRequestDispatcher("infoDetail.jsp").forward(request, response);
    }

    // 获取信息详情
    private InfoDetail getInfoDetail(int infoId) {
        InfoDetail info = new InfoDetail();
        // 修改SQL查询，添加i.id到SELECT中：
        String sql = "SELECT i.id, i.title, i.content, u.username, i.create_time " + // 新增i.id
                "FROM information i JOIN users u ON i.user_id = u.id " +
                "WHERE i.id = ?";

        try (Connection conn = DBConnectionUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, infoId);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                info.setId(rs.getInt("id")); // 设置id
                info.setTitle(rs.getString("title"));
                info.setContent(rs.getString("content"));
                info.setUsername(rs.getString("username"));
                info.setCreateTime(rs.getTimestamp("create_time"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return info;
    }

    // 获取评论列表
    private List<Comment> getCommentList(int infoId) {
        List<Comment> comments = new ArrayList<>();
        String sql = "SELECT c.content, u.username, c.create_time " +
                "FROM comments c JOIN users u ON c.user_id = u.id " +
                "WHERE c.info_id = ?";
        try (Connection conn = DBConnectionUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, infoId);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Comment comment = new Comment();
                comment.setContent(rs.getString("content"));
                comment.setUsername(rs.getString("username"));
                comment.setCreateTime(rs.getTimestamp("create_time"));
                comments.add(comment);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return comments;
    }

    // 获取点赞数
    private int getLikeCount(int infoId) {
        int count = 0;
        String sql = "SELECT COUNT(*) as likeCount FROM likes WHERE info_id = ?";
        try (Connection conn = DBConnectionUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, infoId);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                count = rs.getInt("likeCount");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    // 信息详情实体类（内部类）
    public static class InfoDetail {
        private int id; // 在InfoDetail类顶部添加id字段
        private String title;
        private String content;
        private String username;
        private Timestamp createTime;

        // Getters and Setters
        public String getTitle() { return title; }
        public void setTitle(String title) { this.title = title; }
        public String getContent() { return content; }
        public void setContent(String content) { this.content = content; }
        public String getUsername() { return username; }
        public void setUsername(String username) { this.username = username; }
        public Timestamp getCreateTime() { return createTime; }
        public void setCreateTime(Timestamp createTime) { this.createTime = createTime; }


        public int getId() { return id; } // 需在InfoDetail类中新增id字段并赋值
        public void setId(int id) { this.id = id; }



    }

    // 评论实体类（内部类）
    public static class Comment {
        private String content;
        private String username;
        private Timestamp createTime;

        // Getters and Setters
        public String getContent() { return content; }
        public void setContent(String content) { this.content = content; }
        public String getUsername() { return username; }
        public void setUsername(String username) { this.username = username; }
        public Timestamp getCreateTime() { return createTime; }
        public void setCreateTime(Timestamp createTime) { this.createTime = createTime; }
    }
}